package com.leo.view;

import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.List;

/**
 * @author wangss
 * @date 2023/02/21 19:15
 * @description
 **/
public class FriendListView {

    private TextField toField;
    private Stage stage;
    private VBox vBox;

    public FriendListView(TextField toField) {
        this.toField = toField;
        vBox = new VBox();
        vBox.setSpacing(2);
        Scene scene = new Scene(vBox, 600, 400);
        stage = new Stage();

        stage.setScene(scene);
        // 加载css样式
        stage.setResizable(false);
        stage.sizeToScene();
    }

    public void show() {
        stage.showAndWait();
    }

    public void refresh(List<String> friendList) {
        clear();
        friendList.stream().forEach(str -> {
            Label label = new Label(str);
            vBox.getChildren().add(label);
            label.setOnMouseClicked(event -> {
                toField.setText(str);
                close();
            });
        });
    }

    public void clear() {
        vBox.getChildren().clear();
    }

    public void close() {
        clear();
        stage.close();
    }
}
